package com.yidu.logisticssystem.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.yidu.logisticssystem.entity.LogisticsEvaluate;
import com.yidu.logisticssystem.service.LogisticsEvaluateService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadFileController {
    @Resource
    LogisticsEvaluateService logisticsEvaluateService;
    //上传文件
    @ResponseBody
    @RequestMapping(value = "/uploadFile")
    public Map<String, Object> uploadFile(LogisticsEvaluate logisticsEvaluate, HttpServletRequest request, @Param("file") MultipartFile file) throws IOException {
        Map<String, Object> map = new HashMap<>();
        String savePath = "/D://4321//";//服务器上图片所在的文件夹
        String saveUrl = request.getContextPath() + savePath;//
        String name = file.getOriginalFilename();//图片名称
        file.transferTo(new File(saveUrl + file.getOriginalFilename()));//将图片写到指定的位置
        map.put("code", 0);
        map.put("msg", "上传图片成功");
        Map<String, Object> data = new HashMap<>();
        data.put("src", "/imgs/" + name);//图片路径(切记路径必须要定位到具体的某张图片！！！)

        logisticsEvaluate.setEvaluatePhoto("imgs/"+name);
        logisticsEvaluateService.insert(logisticsEvaluate);

        data.put("title", name);
        map.put("data", data);
        return map;
    }
}